<template>
  <el-dialog title="权限设置:" :visible.sync="showDialogAuthorityTree">
    <div class="select-authority-tree" style="overflow-y: auto;height:400px;">
      <el-tree
        ref="tree"
        :data="AuthorityTreeData"
        show-checkbox
        node-key="code"
        default-expand-all
        :default-checked-keys="HasAuthority"
        :props="defaultProps">
      </el-tree>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="showDialogAuthorityTree = false">取 消</el-button>
      <el-button type="primary" @click="returnBackstagePower()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    name: "powerDialog",
    data() {
      return {
        showDialogAuthorityTree: false,
        authorityArr: [], // 后台所给权限
        HasAuthority: [],
        AuthorityTreeData: [],
        // 权限树名称
        defaultProps: {
          children: 'list',
          label: 'name'
        },
      }
    },
    methods: {
      // 1.获取权限树,分情况,获取用户权限以及获取机构权限,
      getEleNeedPower(authorityArr) {
        this.showDialogAuthorityTree = true
        this.resolveUserAuthorityTree(authorityArr)
      },
      // 2. 将后台给予的权限分配至权限树(进行解析)
      // 解析成权限树
      resolveUserAuthorityTree(authorityArr) {
        // 引用所有code地址
        let authorityArrObj = new Map()
        let authorityTreeArr = []
        let selectObj = {}
        authorityArr.map((currentValue) => {
          // 将当前code存储于map中
          let code = currentValue.code
          authorityArrObj[code] = currentValue
          // 根据code值长度为3时直接push,靠引用
          if (currentValue.code.length === 3) {
            authorityTreeArr.push(authorityArrObj[code])
          } else {
            let auth = currentValue.auth
            if (auth === "1") {
              selectObj[code] = code
            }
            let parentid = currentValue.code.substring(0, currentValue.code.length - 3)
            // 当存在父节点也被选中时,删除父组件
            if (selectObj[parentid]) {
              delete selectObj[parentid]
            }
            if (authorityArrObj[parentid].list) {
              authorityArrObj[parentid].list.push(currentValue)
            }
            else {
              authorityArrObj[parentid].list = []
              authorityArrObj[parentid].list.push(currentValue)
            }
          }
        })
        this.treeObj = authorityArrObj
        this.AuthorityTreeData = authorityTreeArr
        //为了迎合饿了么ui 当子节点存在且父节点也存在的情况下剔除父节点
        this.HasAuthority = Object.keys(selectObj)
      },

      // 3.提交前先将权限遍历,将权限给予后台()
      // 解析用户权限,将用户的祖先权限找到
      getBackstageNeedPower(nowPower) {
        let obj = {}
        let check = true
        // 先拍好顺序,不然肯定找不到爸爸
        nowPower = nowPower.sort()
        nowPower.map((v) => {
          obj[v] = v
          if (v.length >= 6) {
            let parentid = v.substring(0, v.length - 3)
            // 如果当前数据遍历出来的时候存在父权限
            if (!obj[parentid]) {
              obj[parentid] = parentid
              check = false
            }
          }
        })
        // 最后一次遍历,数组中全拥有父组件
        if (check) {
          return Object.keys(obj).map((value) => {
            return this.treeObj[value].id
          })
        } else {
          console.log(obj)
          return this.getBackstageNeedPower(Object.keys(obj))
        }
      },

      // 4.提交权限,即修改权限
      returnBackstagePower() {
        const backNeedPower = this.getBackstageNeedPower(this.$refs.tree.getCheckedKeys())
        this.$emit('backstageNeedPower', backNeedPower)
      },

      // 点击确定5.后台没有出错后关闭弹窗
      hideDialog() {
        this.showDialogAuthorityTree = false
      }
    }
  }
</script>
